package com.example.product.biz.mapper;

import com.example.product.biz.domain.po.ProductDoc;
import com.example.product.biz.domain.po.ProductSku;
import com.example.product.biz.domain.po.SpecGroup;
import com.example.product.biz.domain.vo.ProductDetailVO;
import com.example.product.biz.domain.vo.ProductInfo;
import com.example.product.biz.domain.vo.RecommendProductVO;
import com.example.product.biz.domain.vo.SpecItemVO;
import com.example.product.domain.dto.ItemDTO;
import com.example.product.domain.vo.ProductVO;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface ProductMapper {
    /**
     * 查询商品列表
     * @return
     */
    @Select("select id,name,description,base_price,sales,main_image from product where status=1")
    Page<RecommendProductVO> selectProduct();

    /**
     * 根据商品id查询商品详情
     * @param id
     * @return
     */
    @Select("select id,name,description,base_price from product where id=#{id}")
    ProductDetailVO selectProductById(Integer id) ;

    /**
     * 查询商品图片
     * @param id
     * @return
     */
    @Select("select url from product_image where id= #{id} and image_type=0 ")
    String[] selectImagesByProductId(Integer id);

    /**
     * 查询商品详情图片
     * @param id
     * @return
     */
    @Select("select url from product_image where id= #{id} and image_type=1")
    String[] selectDetailImages(Integer id);

    /**
     * 查询商品规格名称
     * @param productId
     * @return
     */
    @Select("select id,name from spec_group where product_id= #{productId} order by sort_order")
    List<SpecGroup> groupSpecName(Integer productId);

    /**
     * 查询商品规格值
     * @param groupId
     * @return
     */
    @Select("select value as name from spec_item  where group_id= #{groupId}")
    List<SpecItemVO> selectSpecBySpecGroupId(Integer groupId);

    /**
     * 查询商品sku
     * @param procuctId
     * @return
     */
    @Select("select id,tree_id,specs,price,stock,sku_image from product_sku where product_id= #{procuctId}")
    List<ProductSku> selectSkuList(Integer procuctId);

    /**
     * 查询商品规格名称
     * @param groupId
     * @return
     */
    @Select("select name from spec_group where id= #{groupId}")
    String selectGroupName(Integer groupId);

    /**
     * 获取商品规格值
     * @param itemId
     * @return
     */
    @Select("select value from spec_item where id= #{itemId}")
    String selectItemValue(Integer itemId);

    /**
     * 获取商品id
     * @param itemId
     * @return
     */
    @Select("select product_id from product_sku where id= #{itemId}")
    Integer selectProductId(Long itemId);


    @Select("select name,status from product where id= #{productId}")
    ProductVO selectStatusByProductId(Integer productId);


    /**
     * 获取商品信息
     * @param itemId
     * @return
     * */
    @Select("select stock,price,specs,sku_image from product_sku where id= #{itemId}")
    ProductInfo selectProductInfo(Long itemId);

    /**
     * 扣减库存
     * @param itemDTOList
     * */
    void deductStock(List<ItemDTO> itemDTOList);

    /**
     * 恢复库存
     * @param productId
     * @param num
     * */
    @Update("update product_sku set stock=stock+#{num} where id= #{skuId}")
    void restoreStock(Integer skuId, Integer num);

    /**
     * 获取所有商品信息并存到elasticsearch
     * @return
     * */
    @Select("select id,name,description,category_id,base_price,sales,main_image,status from product")
    List<ProductDoc> selectAllProductsForElasticsearch();
}
